from google.appengine.ext import db

from gaeo.controller import BaseController

from model.tag import Tag

class TagController(BaseController):
    def create(self):
        r = Tag(
            name = self.params.get('name', None),
        )
        r.put()
        self.redirect('/tag')

    def destroy(self):
        r = Tag.get(self.params.get('key'))
        if r is not None:
            r.delete()
        self.redirect('/tag')

    def edit(self):
        rec = Tag.get(self.params.get('key'))
        if rec:
            setattr(self, 'name', rec.name)
        else:
            setattr(self, 'name', 'Error')

    def index(self):
        query = Tag.all()
        self.result = query

    def new(self):
        pass

    def show(self):
        rec = Tag.get(self.params.get('key'))
        if rec:
            setattr(self, 'name', rec.name)
        else:
            setattr(self, 'name', 'Error')

    def update(self):
        r = Tag.get(self.params.get('key'))
        r.name = self.params.get('name', None)
        r.put()
        self.redirect('/tag')
